package org.hao.samples.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.hao.samples.model.ExamTestVO;
import org.hao.samples.model.Exam_test;
import org.hao.samples.model.PagerModel;
import org.hao.samples.model.Student;
import org.hao.samples.model.commonMessageResultVO;
import org.hao.samples.model.testVO;
import org.hao.samples.service.ExamTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class ExamTextController {
	
	@Autowired
	private ExamTestService service;

	@RequestMapping("/ExamTests")
	public String ExamTest(Model model) {
		PagerModel<ExamTestVO> list = service.findAllByPage();
		model.addAttribute("ExamTestList", list);
		return "ExamTest/ExamTestList";
	}
	
	@ResponseBody
	@RequestMapping(value = "/getTestById",method =  RequestMethod.POST)
	public ExamTestVO getTestById(Integer testId){
		ExamTestVO ExamTestVO = service.getTestById(testId);
		return ExamTestVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/saveTest",method =  RequestMethod.POST)
	public commonMessageResultVO saveTest(@Valid ExamTestVO examTestVO,BindingResult bindingResult){
		commonMessageResultVO resultVO = new commonMessageResultVO();
		try {
			service.saveExamTest(examTestVO);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/editTest",method =  RequestMethod.POST)
	public commonMessageResultVO editTest(@Valid ExamTestVO examTestVO,BindingResult bindingResult){
		commonMessageResultVO resultVO = new commonMessageResultVO();
		try {
			service.editTest(examTestVO);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultVO;
	}
	
	//RedirectAttributes  这个类能带参数重定向
	@RequestMapping(value = "/deleteTest/{id}",method = RequestMethod.GET)
	public String deleteTest(RedirectAttributes redirectAttributes, @PathVariable Integer id){
		try {
			service.deleteExamTest(id);
			redirectAttributes.addFlashAttribute("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "redirect:/ExamTests";
	}
	
	//生成试卷
	@RequestMapping(value = "/toTest/{subId}",method = RequestMethod.GET)
	public String toTest(HttpServletRequest request,Model model, @PathVariable Integer subId){
		
		HttpSession session = request.getSession();
		Student examUser = (Student) session.getAttribute("examUser");
		Integer userId = examUser.getUser_id();
		List<testVO> listVO = service.findExamTestBySubIdTypeId(subId,userId);
		//获取倒计时
		Integer outTime = service.getoutTime(subId,userId);
		
		model.addAttribute("testList", listVO);
		model.addAttribute("outTime", outTime);
		return "ExamTest/test";
	}
	
	//得到答案
	@ResponseBody
	@RequestMapping(value = "/getAnswer",method = RequestMethod.POST)
	public Map<String, Object> getAnswer(String answerString){
		return service.checkAnswer(answerString);	
	}
}
